"""
This folder contains labels used to collect release artifacts.
"""

load("@com_github_ash2k_bazel_tools//multirun:def.bzl", "multirun")
load("@rules_oci//oci:defs.bzl", "oci_push")
load("//bazel/oci:containers.bzl", "container_reponame", "containers")
load("//bazel/oci:pin.bzl", "oci_sum", "oci_sum_merge")

[
    container_reponame(
        name = container["name"] + "_reponame",
        container_name = container["image_name"],
    )
    for container in containers()
]

[
    genrule(
        name = container["name"] + "_repotag",
        srcs = [
            "//bazel/release:" + container["name"] + "_reponame",
            "//bazel/settings:tag",
        ],
        outs = [container["repotag_file"]],
        cmd = "echo -n ':' | cat $(location //bazel/release:" + container["name"] + "_reponame) - $(location //bazel/settings:tag) > $@",
        visibility = ["//visibility:public"],
    )
    for container in containers()
]

[
    oci_sum(
        name = container["name"] + "_sum",
        image_name = container["image_name"],
        oci = container["oci"],
        repotag_file = container["repotag_file"],
    )
    for container in containers()
]

oci_sum_merge(
    name = "container_sums",
    sums = [
        ":%s_sum" % container["name"]
        for container in containers()
    ],
    visibility = ["//visibility:public"],
)

[
    oci_push(
        name = container["name"] + "_push",
        image = container["oci"],
        remote_tags = "//bazel/settings:tag",
        repository_file = "//bazel/release:" + container["name"] + "_reponame",
    )
    for container in containers()
]

multirun(
    name = "push",
    commands = [
        ":" + container["name"] + "_push"
        for container in containers()
    ],
    jobs = 0,  # execute in parallel
    visibility = ["//visibility:public"],
)

multirun(
    name = "build_and_push",
    commands = [
        "//bazel/release:push",
        "//bazel/devbuild:devbuild",
    ],
    visibility = ["//visibility:public"],
)
